package cn.kgc.vue.commons;

import cn.kgc.vue.exceptions.BusinessException;
import cn.kgc.vue.response.ResponseResult;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 课工场
 * @date 2024/9/20
 * @description  全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {


    // 权限异常
    @ExceptionHandler(UnauthorizedException.class)
    public ResponseResult  UnauthorizedExceptionHandler(UnauthorizedException e){
        e.printStackTrace();
        return  ResponseResult.error().message("权限不足,请联系管理员...");
    }

    // 业务异常
    @ExceptionHandler(BusinessException.class)
    public ResponseResult  businessExceptionHandler(BusinessException e){
        e.printStackTrace();
        return  ResponseResult.error().message(e.getMessage());
    }


    //数据库唯一索引异常处理
    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseResult  duplicateKeyExceptionHandler(DuplicateKeyException e){
        e.printStackTrace();
        return  ResponseResult.error().message("添加的数据已存在,请检查...");
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult  other(Exception e){
           e.printStackTrace();
        return  ResponseResult.error();
    }

}
